<?
/*
	+-----------------------------------------------------------------------------------------+
	| Projeto MonitoraSUS                                                                     |
	+-----------------------------------------------------------------------------------------+
	| Copyright (c) 2005-2008 Secretaria Municipal de Sa�de de Belo Horizonte / Prodabel      |
	|                                                                                         |
	| Analista Respons�vel: Hugo Nathaniel Rodrigues <hugonr@gmail.com>, <hugo@pbh.gov.br> |  
	|                       Consultor - GSFS - Prodabel / SMSA                                |
	| Departamento Respons�vel - SMSA: GPLD - Paulinho <pauloces@pbh.gov.br>                  |
	| Departamento Respons�vel - Prodabel: GSFS - Ricardo Monteiro <monteiro@pbh.gov.br>      |
	| Analista de Informa��o: Marcone <marcone.costa@pbh.gov.br>                              |
	+-----------------------------------------------------------------------------------------+
*/

//Alterado dia: 26/07/2011 16:19; Por: Mauricio Marques

ini_set("default_charset", "ISO-8859-1");
include ("inc.config.php");
$string_conn = "host=$servidorbanco user=$loginbanco password=$senhabanco dbname=$bancodedados";
$conexao = pg_connect($string_conn);
// $conexao = mssql_connect($servidorbanco, $loginbanco, $senhabanco);
// $banco = mssql_select_db($bancodedados, $conexao);
$menu = $_GET["menu"];
$elements = explode("-", $menu);
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=relatorio_acoes.xls");
header("Pragma: no-cache");
header("Expires: 0");
echo "excel.Columns(\"A\").ColumnWidth = 1.86;\n";
echo "excel.Columns(\"B\").ColumnWidth = 21.00;\n";
echo "excel.Columns(\"C\").ColumnWidth = 5.00;\n";


echo "Relat�rio de A��es \t";
?>
<?
if (count($elements) <= 5){
	$compfimsql = "";
	$espaco = "   ";
	if (count($elements) == 1){
		$sql = "select * from eixo where num_eixo = " . $elements[0] .  " and $where";
		$resultado = pg_query( $sql);
		echo "Eixo: \t" . pg_fetch_result($resultado,0,"Des_Eixo") . "\n";
		echo pg_fetch_result($resultado,0,"informacao_eixo") . "\n";
		echo "\n\n";
		$sql = "select count(*) as total from objetivo where $where and num_eixo = " . $elements[0];
		$resultado = pg_query( $sql);
                //Alterado dia: 26/07/2011 16:19; Por: Mauricio Marques
		echo "Quantidade de Projetos/Atividades: \t" . pg_fetch_result($resultado,0,"total") . "\n";
		$compfimsql = " and o.num_eixo = " . $elements[0];
	}
	if (count($elements) == 2){
		$sql = "select * from objetivo where num_objetivo = " . $elements[1] .  " and $where";
		$resultado = pg_query( $sql);
		echo "\nObjetivo: \t" . pg_fetch_result($resultado,0,"des_fant");
		echo "\n" . pg_fetch_result($resultado,0,"des_objetivo");
		$sql2 = "select * from orgao where situacao='A' and num_orgao = " . pg_fetch_result($resultado,0,"num_orgao");
		$resultado2 = pg_query( $sql2);
		echo "\nOrgao Respons�vel: \t" . pg_fetch_result($resultado2,0,"des_orgao");
		echo "\n";
		$compfimsql = " and d.num_objetivo = " . $elements[1];
	}
	if (count($elements) == 3){
		$sql = "select * from diretriz where num_diretriz = " . $elements[2] .  " and $where";
		$resultado = pg_query( $sql);
		echo "\nDiretriz: \t" . pg_fetch_result($resultado,0,"des_diretriz");
		if (pg_fetch_result($resultado,0,"num_func") != ""){
			$sql2 = "select * from funcionario where situacao='A' and num_func = " . pg_fetch_result($resultado,0,"num_func");
			$resultado2 = pg_query( $sql2);
			echo "\nFuncion�rio Respons�vel: \t" . pg_fetch_result($resultado2,0,"nom_func");
		}
		echo "\n";
		$compfimsql = " and m.num_diretriz = " . $elements[2];
	}
	if (count($elements) == 4){
		$sql = "select * from metasgestao where num_metas_gestao = " . $elements[3] .  " and $where";
		$resultado = pg_query( $sql);
		echo "\nObjetivo Espec�fico: \t" . pg_fetch_result($resultado,0,"des_metas_gestao");
		echo "\n";
		$compfimsql = " and mt.num_metas_gestao = " . $elements[2];
	}
	if (count($elements) == 5){
		$sql = "select * from metasgerenciais where num_metas_geren = " . $elements[4] .  " and $where";
		$resultado = pg_query( $sql);
		echo "\nMeta: \t" . pg_fetch_result($resultado,0,"des_metas_geren");
		echo "\n";
		$compfimsql = " and op.num_metas_geren = " . $elements[3];
	}
	if (count($elements) <= 2){
		if (count($elements) == 1){
			$sql = "select count(*) as total from objetivo o, diretriz d where o.exercicio=$exercicio and o.situacao='A' and o.num_objetivo=d.num_objetivo and d.exercicio=$exercicio and d.situacao='A' $compfimsql";
		} else {
			$sql = "select count(*) as total from diretriz d where d.exercicio=$exercicio and d.situacao='A' $compfimsql";
		}
		$resultado = pg_query( $sql);
		echo "\nQuantidade de Diretrizes: \t" . pg_fetch_result($resultado,0,"total");
	}
	if (count($elements) <= 3){
		if (count($elements) == 1){
			$sql = "select count(*) as total from objetivo o, diretriz d, metasgestao m where o.exercicio=$exercicio and o.situacao='A' and o.num_objetivo=d.num_objetivo and d.exercicio=$exercicio and d.situacao='A' and m.exercicio=$exercicio and m.situacao='A' and m.num_diretriz=d.num_diretriz $compfimsql";
		} elseif (count($elements) == 2){
			$sql = "select count(*) as total from diretriz d, metasgestao m where d.exercicio=$exercicio and d.situacao='A' and m.exercicio=$exercicio and m.situacao='A' and m.num_diretriz=d.num_diretriz $compfimsql";
		} elseif (count($elements) == 3){
			$sql = "select count(*) as total from metasgestao m where m.exercicio=$exercicio and m.situacao='A' $compfimsql";
		}
		$resultado = pg_query( $sql);
		echo "\nQuantidade de Objetivos espec�ficos: \t" . pg_fetch_result($resultado,0,"total");
	}
	if (count($elements) <= 4){
		if (count($elements) == 1){
			$sql = "select count(*) as total from objetivo o, diretriz d, metasgestao m, metasgerenciais mt where o.exercicio=$exercicio and o.situacao='A' and o.num_objetivo=d.num_objetivo and d.exercicio=$exercicio and d.situacao='A' and m.exercicio=$exercicio and m.situacao='A' and mt.exercicio=$exercicio and mt.situacao='A' and m.num_diretriz=d.num_diretriz and m.num_metas_gestao=mt.num_metas_gestao $compfimsql";
		} elseif (count($elements) == 2){
			$sql = "select count(*) as total from diretriz d, metasgestao m, metasgerenciais mt where  d.exercicio=$exercicio and d.situacao='A' and m.exercicio=$exercicio and m.situacao='A' and mt.exercicio=$exercicio and mt.situacao='A' and m.num_diretriz=d.num_diretriz and m.num_metas_gestao=mt.num_metas_gestao $compfimsql";
		} elseif (count($elements) == 3){
			$sql = "select count(*) as total from metasgestao m, metasgerenciais mt where  m.exercicio=$exercicio and m.situacao='A' and mt.exercicio=$exercicio and mt.situacao='A' and  m.num_metas_gestao=mt.num_metas_gestao $compfimsql";
		} elseif (count($elements) == 4){
			$sql = "select count(*) as total from metasgerenciais mt where  mt.exercicio=$exercicio and mt.situacao='A' $compfimsql";
		}	
		$resultado = pg_query( $sql);
		echo "\nQuantidade de Metas: \t" . pg_fetch_result($resultado,0,"total");
	}
	
	if (count($elements) == 1){
		$sql = "select count(*) as total from objetivo o, diretriz d, metasgestao m, metasgerenciais mt, operacao op where o.exercicio=$exercicio and o.situacao='A' and o.num_objetivo=d.num_objetivo and d.exercicio=$exercicio and d.situacao='A' and m.exercicio=$exercicio and m.situacao='A' and mt.exercicio=$exercicio and mt.situacao='A' and op.exercicio=$exercicio and op.situacao='A' and m.num_diretriz=d.num_diretriz and m.num_metas_gestao=mt.num_metas_gestao and op.num_metas_geren=mt.num_metas_geren $compfimsql";
	} elseif (count($elements) == 2){
		$sql = "select count(*) as total from diretriz d, metasgestao m, metasgerenciais mt, operacao op where d.exercicio=$exercicio and d.situacao='A' and m.exercicio=$exercicio and m.situacao='A' and mt.exercicio=$exercicio and mt.situacao='A' and op.exercicio=$exercicio and op.situacao='A' and m.num_diretriz=d.num_diretriz and m.num_metas_gestao=mt.num_metas_gestao and op.num_metas_geren=mt.num_metas_geren $compfimsql";
	} elseif (count($elements) == 3){
		$sql = "select count(*) as total from metasgestao m, metasgerenciais mt, operacao op where  m.exercicio=$exercicio and m.situacao='A' and mt.exercicio=$exercicio and mt.situacao='A' and op.exercicio=$exercicio and op.situacao='A' and m.num_metas_gestao=mt.num_metas_gestao and op.num_metas_geren=mt.num_metas_geren $compfimsql";
	} elseif (count($elements) == 4){
		$sql = "select count(*) as total from metasgerenciais mt, operacao op where   mt.exercicio=$exercicio and mt.situacao='A' and op.exercicio=$exercicio and op.situacao='A' and  op.num_metas_geren=mt.num_metas_geren $compfimsql";
	} elseif (count($elements) == 5){
		$sql = "select count(*) as total from operacao op where   op.exercicio=$exercicio and op.situacao='A' $compfimsql";
	}
	$resultado = pg_query( $sql);
	echo "\nQuantidade de Opera��es: \t" . pg_fetch_result($resultado,0,"total");


	if (count($elements) == 1){
		$sql = "select count(*) as total from objetivo o, diretriz d, metasgestao m, metasgerenciais mt, operacao op, acao a where o.exercicio=$exercicio and o.situacao='A' and o.num_objetivo=d.num_objetivo and d.exercicio=$exercicio and d.situacao='A' and m.exercicio=$exercicio and m.situacao='A' and mt.exercicio=$exercicio and mt.situacao='A' and op.exercicio=$exercicio and op.situacao='A' and a.exercicio=$exercicio and a.situacao='A' and m.num_diretriz=d.num_diretriz and m.num_metas_gestao=mt.num_metas_gestao and op.num_metas_geren=mt.num_metas_geren and a.num_operacao=op.num_operacao $compfimsql";
	} elseif (count($elements) == 2){
		$sql = "select count(*) as total from diretriz d, metasgestao m, metasgerenciais mt, operacao op, acao a where d.exercicio=$exercicio and d.situacao='A' and m.exercicio=$exercicio and m.situacao='A' and mt.exercicio=$exercicio and mt.situacao='A' and op.exercicio=$exercicio and op.situacao='A' and a.exercicio=$exercicio and a.situacao='A' and m.num_diretriz=d.num_diretriz and m.num_metas_gestao=mt.num_metas_gestao and op.num_metas_geren=mt.num_metas_geren and a.num_operacao=op.num_operacao $compfimsql";
	} elseif (count($elements) == 3){
		$sql = "select count(*) as total from metasgestao m, metasgerenciais mt, operacao op, acao a where m.exercicio=$exercicio and m.situacao='A' and mt.exercicio=$exercicio and mt.situacao='A' and op.exercicio=$exercicio and op.situacao='A' and a.exercicio=$exercicio and a.situacao='A' and m.num_metas_gestao=mt.num_metas_gestao and op.num_metas_geren=mt.num_metas_geren and a.num_operacao=op.num_operacao $compfimsql";
	} elseif (count($elements) == 4){
		$sql = "select count(*) as total from metasgerenciais mt, operacao op, acao a where  mt.exercicio=$exercicio and mt.situacao='A' and op.exercicio=$exercicio and op.situacao='A' and a.exercicio=$exercicio and a.situacao='A' and op.num_metas_geren=mt.num_metas_geren and a.num_operacao=op.num_operacao $compfimsql";
	} elseif (count($elements) == 5){
		$sql = "select count(*) as total from operacao op, acao a where  op.exercicio=$exercicio and op.situacao='A' and a.exercicio=$exercicio and a.situacao='A' and a.num_operacao=op.num_operacao $compfimsql";
	}
	$resultado = pg_query( $sql);
	echo "\nQuantidade de A��es: \t" . pg_fetch_result($resultado,0,"total");

	if (count($elements) == 1){
		$sql = "select count(*) as total from objetivo o, diretriz d, metasgestao m, metasgerenciais mt, operacao op, acao a where o.exercicio=$exercicio and o.situacao='A' and o.num_objetivo=d.num_objetivo and d.exercicio=$exercicio and d.situacao='A' and m.exercicio=$exercicio and m.situacao='A' and mt.exercicio=$exercicio and mt.situacao='A' and op.exercicio=$exercicio and op.situacao='A' and a.exercicio=$exercicio and a.situacao='A' and m.num_diretriz=d.num_diretriz and m.num_metas_gestao=mt.num_metas_gestao and op.num_metas_geren=mt.num_metas_geren and a.num_operacao=op.num_operacao and a.num_acao in (select ap.num_acao from acao_prazo ap where ap.dt_real_fina is not null and ap.situacao='A' and ap.exercicio=$exercicio) $compfimsql";
	} elseif (count($elements) == 2){
		$sql = "select count(*) as total from diretriz d, metasgestao m, metasgerenciais mt, operacao op, acao a where d.exercicio=$exercicio and d.situacao='A' and m.exercicio=$exercicio and m.situacao='A' and mt.exercicio=$exercicio and mt.situacao='A' and op.exercicio=$exercicio and op.situacao='A' and a.exercicio=$exercicio and a.situacao='A' and m.num_diretriz=d.num_diretriz and m.num_metas_gestao=mt.num_metas_gestao and op.num_metas_geren=mt.num_metas_geren and a.num_operacao=op.num_operacao and a.num_acao in (select ap.num_acao from acao_prazo ap where ap.dt_real_fina is not null and ap.situacao='A' and ap.exercicio=$exercicio) $compfimsql";
	} elseif (count($elements) == 3){
		$sql = "select count(*) as total from metasgestao m, metasgerenciais mt, operacao op, acao a where m.exercicio=$exercicio and m.situacao='A' and mt.exercicio=$exercicio and mt.situacao='A' and op.exercicio=$exercicio and op.situacao='A' and a.exercicio=$exercicio and a.situacao='A' and m.num_metas_gestao=mt.num_metas_gestao and op.num_metas_geren=mt.num_metas_geren and a.num_operacao=op.num_operacao and a.num_acao in (select ap.num_acao from acao_prazo ap where ap.dt_real_fina is not null and ap.situacao='A' and ap.exercicio=$exercicio) $compfimsql";
	} elseif (count($elements) == 4){
		$sql = "select count(*) as total from  metasgerenciais mt, operacao op, acao a where  mt.exercicio=$exercicio and mt.situacao='A' and op.exercicio=$exercicio and op.situacao='A' and a.exercicio=$exercicio and a.situacao='A' and op.num_metas_geren=mt.num_metas_geren and a.num_operacao=op.num_operacao and a.num_acao in (select ap.num_acao from acao_prazo ap where ap.dt_real_fina is not null and ap.situacao='A' and ap.exercicio=$exercicio) $compfimsql";
	} elseif (count($elements) == 5){
		$sql = "select count(*) as total from   operacao op, acao a where  op.exercicio=$exercicio and op.situacao='A' and a.exercicio=$exercicio and a.situacao='A' and  a.num_operacao=op.num_operacao and a.num_acao in (select ap.num_acao from acao_prazo ap where ap.dt_real_fina is not null and ap.situacao='A' and ap.exercicio=$exercicio) $compfimsql";
	}

	$resultado = pg_query( $sql);
	echo "\nQuantidade de A��es Fechadas: \t" . pg_fetch_result($resultado,0,"total");

	if (count($elements) == 1){
		$sql = "select count(*) as total from objetivo o, diretriz d, metasgestao m, metasgerenciais mt, operacao op, acao a where o.exercicio=$exercicio and o.situacao='A' and o.num_objetivo=d.num_objetivo and d.exercicio=$exercicio and d.situacao='A' and m.exercicio=$exercicio and m.situacao='A' and mt.exercicio=$exercicio and mt.situacao='A' and op.exercicio=$exercicio and op.situacao='A' and a.exercicio=$exercicio and a.situacao='A' and m.num_diretriz=d.num_diretriz and m.num_metas_gestao=mt.num_metas_gestao and op.num_metas_geren=mt.num_metas_geren and a.num_operacao=op.num_operacao and a.num_acao in (select ap.num_acao from acao_prazo ap where ap.dt_real_fina is null and ap.situacao='A' and dt_prev_fina<getdate() and dt_real_fina is null and ap.exercicio=$exercicio) $compfimsql";
	} elseif (count($elements) == 2){
		$sql = "select count(*) as total from  diretriz d, metasgestao m, metasgerenciais mt, operacao op, acao a where d.exercicio=$exercicio and d.situacao='A' and m.exercicio=$exercicio and m.situacao='A' and mt.exercicio=$exercicio and mt.situacao='A' and op.exercicio=$exercicio and op.situacao='A' and a.exercicio=$exercicio and a.situacao='A' and m.num_diretriz=d.num_diretriz and m.num_metas_gestao=mt.num_metas_gestao and op.num_metas_geren=mt.num_metas_geren and a.num_operacao=op.num_operacao and a.num_acao in (select ap.num_acao from acao_prazo ap where ap.dt_real_fina is null and ap.situacao='A' and dt_prev_fina<getdate() and dt_real_fina is null and ap.exercicio=$exercicio) $compfimsql";
	} elseif (count($elements) == 3){
		$sql = "select count(*) as total from metasgestao m, metasgerenciais mt, operacao op, acao a where m.exercicio=$exercicio and m.situacao='A' and mt.exercicio=$exercicio and mt.situacao='A' and op.exercicio=$exercicio and op.situacao='A' and a.exercicio=$exercicio and a.situacao='A' and m.num_metas_gestao=mt.num_metas_gestao and op.num_metas_geren=mt.num_metas_geren and a.num_operacao=op.num_operacao and a.num_acao in (select ap.num_acao from acao_prazo ap where ap.dt_real_fina is null and ap.situacao='A' and dt_prev_fina<getdate() and dt_real_fina is null and ap.exercicio=$exercicio) $compfimsql";
	} elseif (count($elements) == 4){
		$sql = "select count(*) as total from metasgerenciais mt, operacao op, acao a where  mt.exercicio=$exercicio and mt.situacao='A' and op.exercicio=$exercicio and op.situacao='A' and a.exercicio=$exercicio and a.situacao='A' and op.num_metas_geren=mt.num_metas_geren and a.num_operacao=op.num_operacao and a.num_acao in (select ap.num_acao from acao_prazo ap where ap.dt_real_fina is null and ap.situacao='A' and dt_prev_fina<getdate() and dt_real_fina is null and ap.exercicio=$exercicio) $compfimsql";
	} elseif (count($elements) == 5){
		$sql = "select count(*) as total from operacao op, acao a where  op.exercicio=$exercicio and op.situacao='A' and a.exercicio=$exercicio and a.situacao='A' and a.num_operacao=op.num_operacao and a.num_acao in (select ap.num_acao from acao_prazo ap where ap.dt_real_fina is null and ap.situacao='A' and dt_prev_fina<getdate() and dt_real_fina is null and ap.exercicio=$exercicio) $compfimsql";
	}

	$resultado = pg_query( $sql);
	echo "\nQuantidade de A��es Atrasadas: \t" . pg_fetch_result($resultado,0,"total");

	if (count($elements) == 1){
		$sql = "select count(*) as total from objetivo o, diretriz d, metasgestao m, metasgerenciais mt, operacao op, acao a where o.exercicio=$exercicio and o.situacao='A' and o.num_objetivo=d.num_objetivo and d.exercicio=$exercicio and d.situacao='A' and m.exercicio=$exercicio and m.situacao='A' and mt.exercicio=$exercicio and mt.situacao='A' and op.exercicio=$exercicio and op.situacao='A' and a.exercicio=$exercicio and a.situacao='A' and m.num_diretriz=d.num_diretriz and m.num_metas_gestao=mt.num_metas_gestao and op.num_metas_geren=mt.num_metas_geren and a.num_operacao=op.num_operacao and a.num_acao in (select ap.num_acao from acao_prazo ap where ap.dt_real_fina is null and ap.situacao='A' and dt_prev_fina>=getdate() and ap.exercicio=$exercicio) $compfimsql";
	} elseif (count($elements) == 2){
		$sql = "select count(*) as total from diretriz d, metasgestao m, metasgerenciais mt, operacao op, acao a where d.exercicio=$exercicio and d.situacao='A' and m.exercicio=$exercicio and m.situacao='A' and mt.exercicio=$exercicio and mt.situacao='A' and op.exercicio=$exercicio and op.situacao='A' and a.exercicio=$exercicio and a.situacao='A' and m.num_diretriz=d.num_diretriz and m.num_metas_gestao=mt.num_metas_gestao and op.num_metas_geren=mt.num_metas_geren and a.num_operacao=op.num_operacao and a.num_acao in (select ap.num_acao from acao_prazo ap where ap.dt_real_fina is null and ap.situacao='A' and dt_prev_fina>=getdate() and ap.exercicio=$exercicio) $compfimsql";
	} elseif (count($elements) == 3){
		$sql = "select count(*) as total from metasgestao m, metasgerenciais mt, operacao op, acao a where m.exercicio=$exercicio and m.situacao='A' and mt.exercicio=$exercicio and mt.situacao='A' and op.exercicio=$exercicio and op.situacao='A' and a.exercicio=$exercicio and a.situacao='A' and m.num_metas_gestao=mt.num_metas_gestao and op.num_metas_geren=mt.num_metas_geren and a.num_operacao=op.num_operacao and a.num_acao in (select ap.num_acao from acao_prazo ap where ap.dt_real_fina is null and ap.situacao='A' and dt_prev_fina>=getdate() and ap.exercicio=$exercicio) $compfimsql";
	} elseif (count($elements) == 4){
		$sql = "select count(*) as total from metasgerenciais mt, operacao op, acao a where  mt.exercicio=$exercicio and mt.situacao='A' and op.exercicio=$exercicio and op.situacao='A' and a.exercicio=$exercicio and a.situacao='A' and op.num_metas_geren=mt.num_metas_geren and a.num_operacao=op.num_operacao and a.num_acao in (select ap.num_acao from acao_prazo ap where ap.dt_real_fina is null and ap.situacao='A' and dt_prev_fina>=getdate() and ap.exercicio=$exercicio) $compfimsql";
	} elseif (count($elements) == 5){
		$sql = "select count(*) as total from  operacao op, acao a where  op.exercicio=$exercicio and op.situacao='A' and a.exercicio=$exercicio and a.situacao='A' and a.num_operacao=op.num_operacao and a.num_acao in (select ap.num_acao from acao_prazo ap where ap.dt_real_fina is null and ap.situacao='A' and dt_prev_fina>=getdate() and ap.exercicio=$exercicio) $compfimsql";
	}

	$resultado = pg_query( $sql);
	echo "\nQuantidade de A��es no prazo: \t" . pg_fetch_result($resultado,0,"total");

	if (count($elements) == 1){
		$sql = "select count(*) as total from objetivo o, diretriz d, metasgestao m, metasgerenciais mt, operacao op, acao a where o.exercicio=$exercicio and o.situacao='A' and o.num_objetivo=d.num_objetivo and d.exercicio=$exercicio and d.situacao='A' and m.exercicio=$exercicio and m.situacao='A' and mt.exercicio=$exercicio and mt.situacao='A' and op.exercicio=$exercicio and op.situacao='A' and a.exercicio=$exercicio and a.situacao='A' and m.num_diretriz=d.num_diretriz and m.num_metas_gestao=mt.num_metas_gestao and op.num_metas_geren=mt.num_metas_geren and a.num_operacao=op.num_operacao and a.num_acao not in (select ap.num_acao from acao_prazo ap where ap.situacao='A' and ap.exercicio=$exercicio) $compfimsql";
	} elseif (count($elements) == 2){
		$sql = "select count(*) as total from diretriz d, metasgestao m, metasgerenciais mt, operacao op, acao a where d.exercicio=$exercicio and d.situacao='A' and m.exercicio=$exercicio and m.situacao='A' and mt.exercicio=$exercicio and mt.situacao='A' and op.exercicio=$exercicio and op.situacao='A' and a.exercicio=$exercicio and a.situacao='A' and m.num_diretriz=d.num_diretriz and m.num_metas_gestao=mt.num_metas_gestao and op.num_metas_geren=mt.num_metas_geren and a.num_operacao=op.num_operacao and a.num_acao not in (select ap.num_acao from acao_prazo ap where ap.situacao='A' and ap.exercicio=$exercicio) $compfimsql";
	} elseif (count($elements) == 3){
		$sql = "select count(*) as total from metasgestao m, metasgerenciais mt, operacao op, acao a where m.exercicio=$exercicio and m.situacao='A' and mt.exercicio=$exercicio and mt.situacao='A' and op.exercicio=$exercicio and op.situacao='A' and a.exercicio=$exercicio and a.situacao='A' and m.num_metas_gestao=mt.num_metas_gestao and op.num_metas_geren=mt.num_metas_geren and a.num_operacao=op.num_operacao and a.num_acao not in (select ap.num_acao from acao_prazo ap where ap.situacao='A' and ap.exercicio=$exercicio) $compfimsql";
	} elseif (count($elements) == 4){
		$sql = "select count(*) as total from  metasgerenciais mt, operacao op, acao a where  mt.exercicio=$exercicio and mt.situacao='A' and op.exercicio=$exercicio and op.situacao='A' and a.exercicio=$exercicio and a.situacao='A' and  op.num_metas_geren=mt.num_metas_geren and a.num_operacao=op.num_operacao and a.num_acao not in (select ap.num_acao from acao_prazo ap where ap.situacao='A' and ap.exercicio=$exercicio) $compfimsql";
	} elseif (count($elements) == 5){
		$sql = "select count(*) as total from operacao op, acao a where op.exercicio=$exercicio and op.situacao='A' and a.exercicio=$exercicio and a.situacao='A' and a.num_operacao=op.num_operacao and a.num_acao not in (select ap.num_acao from acao_prazo ap where ap.situacao='A' and ap.exercicio=$exercicio) $compfimsql";
	}

	$resultado = pg_query( $sql);
	echo "\nQuantidade de A��es sem prazo definido: \t" . pg_fetch_result($resultado,0,"total");
	echo "\n\n";
	echo "Texto em  negrito  indica a�ao em atraso\nTexto em  it�lico  indica a�ao fechada\nTexto sem formata�ao indica a�ao aberta mas no prazo.\n\n";
	if (count($elements) == 1){ // eixo
		$sql = "select * from eixo where num_eixo = " . $elements[0] .  " and $where";
		$resultado = pg_query( $sql);
		echo "\nEixo: " . pg_fetch_result($resultado,0,"des_eixo");
		$num_eixo = pg_fetch_result($resultado,0,"num_eixo");
		$sql = "select * from objetivo where num_eixo = " . $num_eixo . " and $where";
		$resultado = pg_query( $sql);
		while ($rs = pg_fetch_array($resultado)){
			echo "\n   Objetivo Geral " . $rs["num_objetivo"] . ": ";
			echo $rs["des_objetivo"];
			$num_objetivo = $rs["num_objetivo"];
			$sql2 = "select * from diretriz where $where and num_objetivo = $num_objetivo";
			$resultado2 = pg_query( $sql2);
			while ($rs2 = pg_fetch_array($resultado2)){
				echo "\n      Diretriz " . $rs2["num_diretriz"] . ": ";
				echo $rs2["des_diretriz"];
				$num_diretriz = $rs2["num_diretriz"];
				$sql3 = "select * from metasgestao where $where and num_diretriz = $num_diretriz";
				$resultado3 = pg_query( $sql3);
				while ($rs3 = pg_fetch_array($resultado3)){
					echo "\n         Objetivo Espec�fico " . $rs3["num_metas_gestao"] . ": ";
					echo $rs3["des_metas_gestao"];
					$num_metas_gestao = $rs3["num_metas_gestao"];
					$sql4 = "select * from metasgerenciais where $where and num_metas_gestao = $num_metas_gestao";
					$resultado4 = pg_query( $sql4);
					while ($rs4 = pg_fetch_array($resultado4)){
						echo "\n            Meta " . $rs4["num_metas_geren"] . ": ";
						echo $rs4["des_metas_geren"];
						$num_metas_geren = $rs4["num_metas_geren"];
						$sql5 = "select * from operacao where $where and num_metas_geren = $num_metas_geren";
						$resultado5 = pg_query( $sql5);
						while ($rs5 = pg_fetch_array($resultado5)){
							echo "\n               Opera�ao " . $rs5["num_operacao"] . ": ";
							echo $rs5["des_operacao"];
							$num_operacao = $rs5["num_operacao"];
							$sql6 = "select * from acao where $where and num_operacao = $num_operacao";
							$resultado6 = pg_query( $sql6);
							while ($rs6 = pg_fetch_array($resultado6)){
								$num_acao = $rs6["num_acao"];
								$sql7 = "select count(*) as total from acao_prazo where $where and dt_real_fina is not null and num_acao=$num_acao";
								$resultado7 = pg_query( $sql7);
								$sql8 = "select count(*) as total from acao_prazo where $where and dt_prev_fina<getdate() and dt_real_fina is null and num_acao=$num_acao";
								$resultado8 = pg_query( $sql8);
								if (pg_fetch_result($resultado7,0,"total") > 0){
									echo " ";
								}
								if (pg_fetch_result($resultado8,0,"total") > 0){
									echo " ";
								}
								$sql9 = "select data=case when dt_real_fina is not null then convert(char(10),dt_real_fina,103) else convert(char(10),dt_prev_fina,103) end from acao_prazo where $where and num_acao = $num_acao";
								$resultado9 = pg_query( $sql9);
								echo "\n                  A��o " . $rs6["num_acao"] . ": ";
								echo $rs6["des_acao"] . " \t" . trim(pg_fetch_result($resultado9,0,"data"));
							}
						}
					}
				}
			}
		}			
	} // fim eixo

	if (count($elements) == 2){ // objetivo geral
			$sql = "select * from objetivo where num_objetivo = " . $elements[1] .  " and $where";
			$resultado = pg_query( $sql);
			echo "\n   Objetivo Geral " . pg_fetch_result($resultado,0,"num_objetivo") . ": ";
			echo pg_fetch_result($resultado,0,"des_objetivo");
			$num_objetivo = pg_fetch_result($resultado,0,"num_objetivo");
			$sql2 = "select * from diretriz where $where and num_objetivo = $num_objetivo";
			$resultado2 = pg_query( $sql2);
			while ($rs2 = pg_fetch_array($resultado2)){
				echo "\n      Diretriz " . $rs2["num_diretriz"] . ": ";
				echo $rs2["des_diretriz"];
				$num_diretriz = $rs2["num_diretriz"];
				$sql3 = "select * from metasgestao where $where and num_diretriz = $num_diretriz";
				$resultado3 = pg_query( $sql3);
				while ($rs3 = pg_fetch_array($resultado3)){
					echo "\n         Objetivo Espec�fico " . $rs3["num_metas_gestao"] . ": ";
					echo $rs3["des_metas_gestao"];
					$num_metas_gestao = $rs3["num_metas_gestao"];
					$sql4 = "select * from metasgerenciais where $where and num_metas_gestao = $num_metas_gestao";
					$resultado4 = pg_query( $sql4);
					while ($rs4 = pg_fetch_array($resultado4)){
						echo "\n            Meta " . $rs4["num_metas_geren"] . ": ";
						echo $rs4["des_metas_geren"];
						$num_metas_geren = $rs4["num_metas_geren"];
						$sql5 = "select * from operacao where $where and num_metas_geren = $num_metas_geren";
						$resultado5 = pg_query( $sql5);
						while ($rs5 = pg_fetch_array($resultado5)){
							echo "\n               Opera�ao " . $rs5["num_operacao"] . ": ";
							echo $rs5["des_operacao"];
							$num_operacao = $rs5["num_operacao"];
							$sql6 = "select * from acao where $where and num_operacao = $num_operacao";
							$resultado6 = pg_query( $sql6);
							while ($rs6 = pg_fetch_array($resultado6)){
								$num_acao = $rs6["num_acao"];
								$sql7 = "select count(*) as total from acao_prazo where $where and dt_real_fina is not null and num_acao=$num_acao";
								$resultado7 = pg_query( $sql7);
								$sql8 = "select count(*) as total from acao_prazo where $where and dt_prev_fina<getdate() and dt_real_fina is null and num_acao=$num_acao";
								$resultado8 = pg_query( $sql8);
								if (pg_fetch_result($resultado7,0,"total") > 0){
									echo " ";
								}
								if (pg_fetch_result($resultado8,0,"total") > 0){
									echo " ";
								}
								$sql9 = "select data=case when dt_real_fina is not null then convert(char(10),dt_real_fina,103) else convert(char(10),dt_prev_fina,103) end from acao_prazo where $where and num_acao = $num_acao";
								$resultado9 = pg_query( $sql9);
								echo "\n                  A�ao " . $rs6["num_acao"] . ": ";
								echo $rs6["des_acao"] . " \t" . trim(pg_fetch_result($resultado9,0,"data"));

							}
						}
					}
				}
			}
	} // fim objetivo

	if (count($elements) == 3){ // diretriz
			$sql2 = "select * from diretriz where $where and num_diretriz = " . $elements[2];
			$resultado2 = pg_query( $sql2);
			while ($rs2 = pg_fetch_array($resultado2)){
				echo "\n   Diretriz " . $rs2["num_diretriz"] . ": ";
				echo $rs2["des_diretriz"];
				$num_diretriz = $rs2["num_diretriz"];
				$sql3 = "select * from metasgestao where $where and num_diretriz = $num_diretriz";
				$resultado3 = pg_query( $sql3);
				while ($rs3 = pg_fetch_array($resultado3)){
					echo "\n      Objetivo Espec�fico " . $rs3["num_metas_gestao"] . ": ";
					echo $rs3["des_metas_gestao"];
					$num_metas_gestao = $rs3["num_metas_gestao"];
					$sql4 = "select * from metasgerenciais where $where and num_metas_gestao = $num_metas_gestao";
					$resultado4 = pg_query( $sql4);
					while ($rs4 = pg_fetch_array($resultado4)){
						echo "\n         Meta " . $rs4["num_metas_geren"] . ": ";
						echo $rs4["des_metas_geren"];
						$num_metas_geren = $rs4["num_metas_geren"];
						$sql5 = "select * from operacao where $where and num_metas_geren = $num_metas_geren";
						$resultado5 = pg_query( $sql5);
						while ($rs5 = pg_fetch_array($resultado5)){
							echo "\n            Opera�ao " . $rs5["num_operacao"] . ": ";
							echo $rs5["des_operacao"];
							$num_operacao = $rs5["num_operacao"];
							$sql6 = "select * from acao where $where and num_operacao = $num_operacao";
							$resultado6 = pg_query( $sql6);
							while ($rs6 = pg_fetch_array($resultado6)){
								$num_acao = $rs6["num_acao"];
								$sql7 = "select count(*) as total from acao_prazo where $where and dt_real_fina is not null and num_acao=$num_acao";
								$resultado7 = pg_query( $sql7);
								$sql8 = "select count(*) as total from acao_prazo where $where and dt_prev_fina<getdate() and dt_real_fina is null and num_acao=$num_acao";
								$resultado8 = pg_query( $sql8);
								if (pg_fetch_result($resultado7,0,"total") > 0){
									echo " ";
								}
								if (pg_fetch_result($resultado8,0,"total") > 0){
									echo " ";
								}
								$sql9 = "select data=case when dt_real_fina is not null then convert(char(10),dt_real_fina,103) else convert(char(10),dt_prev_fina,103) end from acao_prazo where $where and num_acao = $num_acao";
								$resultado9 = pg_query( $sql9);
								echo "\n               A�ao " . $rs6["num_acao"] . ": ";
								echo $rs6["des_acao"] . " \t" . trim(pg_fetch_result($resultado9,0,"data"));
	
							}
						}
					}
				}
			}
	} // fim diretriz

	if (count($elements) == 4){ // objetivo especifico
				$sql3 = "select * from metasgestao where $where and num_metas_gestao = " . $elements[3];
				$resultado3 = pg_query( $sql3);
				while ($rs3 = pg_fetch_array($resultado3)){
					echo "\n   Objetivo Espec�fico " . $rs3["num_metas_gestao"] . ": ";
					echo $rs3["des_metas_gestao"];
					$num_metas_gestao = $rs3["num_metas_gestao"];
					$sql4 = "select * from metasgerenciais where $where and num_metas_gestao = $num_metas_gestao";
					$resultado4 = pg_query( $sql4);
					while ($rs4 = pg_fetch_array($resultado4)){
						echo "\n      Meta " . $rs4["num_metas_geren"] . ": ";
						echo $rs4["des_metas_geren"];
						$num_metas_geren = $rs4["num_metas_geren"];
						$sql5 = "select * from operacao where $where and num_metas_geren = $num_metas_geren";
						$resultado5 = pg_query( $sql5);
						while ($rs5 = pg_fetch_array($resultado5)){
							echo "\n         Opera�ao " . $rs5["num_operacao"] . ": ";
							echo $rs5["des_operacao"];
							$num_operacao = $rs5["num_operacao"];
							$sql6 = "select * from acao where $where and num_operacao = $num_operacao";
							$resultado6 = pg_query( $sql6);
							while ($rs6 = pg_fetch_array($resultado6)){
								$num_acao = $rs6["num_acao"];
								$sql7 = "select count(*) as total from acao_prazo where $where and dt_real_fina is not null and num_acao=$num_acao";
								$resultado7 = pg_query( $sql7);
								$sql8 = "select count(*) as total from acao_prazo where $where and dt_prev_fina<getdate() and dt_real_fina is null and num_acao=$num_acao";
								$resultado8 = pg_query( $sql8);
								if (pg_fetch_result($resultado7,0,"total") > 0){
									echo " ";
								}
								if (pg_fetch_result($resultado8,0,"total") > 0){
									echo " ";
								}
								$sql9 = "select data=case when dt_real_fina is not null then convert(char(10),dt_real_fina,103) else convert(char(10),dt_prev_fina,103) end from acao_prazo where $where and num_acao = $num_acao";
								$resultado9 = pg_query( $sql9);
								echo "\n            A�ao " . $rs6["num_acao"] . ": ";
								echo $rs6["des_acao"] . " \t" . trim(pg_fetch_result($resultado9,0,"data"));
							}
						}
					}
				}
	} // fim objetivo especifico

	if (count($elements) == 5){ // metas
					$sql4 = "select * from metasgerenciais where $where and Num_Metas_Geren = " . $elements[4];
					$resultado4 = pg_query( $sql4);
					while ($rs4 = pg_fetch_array($resultado4)){
						echo "\n   Meta " . $rs4["num_metas_geren"] . ": ";
						echo $rs4["des_metas_geren"];
						$num_metas_geren = $rs4["num_metas_geren"];
						$sql5 = "select * from operacao where $where and num_metas_geren = $num_metas_geren";
						$resultado5 = pg_query( $sql5);
						while ($rs5 = pg_fetch_array($resultado5)){
							echo "\n      Opera�ao " . $rs5["num_operacao"] . ": ";
							echo $rs5["des_operacao"];
							$num_operacao = $rs5["num_operacao"];
							$sql6 = "select * from acao where $where and num_operacao = $num_operacao";
							$resultado6 = pg_query( $sql6);
							while ($rs6 = pg_fetch_array($resultado6)){
								$num_acao = $rs6["num_acao"];
								$sql7 = "select count(*) as total from acao_prazo where $where and dt_real_fina is not null and num_acao=$num_acao";
								$resultado7 = pg_query( $sql7);
								$sql8 = "select count(*) as total from acao_prazo where $where and dt_prev_fina<getdate() and dt_real_fina is null and num_acao=$num_acao";
								$resultado8 = pg_query( $sql8);
								
								if (pg_fetch_result($resultado7,0,"total") > 0){
									echo " ";
								}
								if (pg_fetch_result($resultado8,0,"total") > 0){
									echo " ";
								}
								$sql9 = "select data=case when dt_real_fina is not null then convert(char(10),dt_real_fina,103) else convert(char(10),dt_prev_fina,103) end from acao_prazo where $where and num_acao = $num_acao";
								$resultado9 = pg_query( $sql9);
								// echo "\n            A�ao " . $rs6["Num_Acao"] . ": ";
								$des_acao = "\n            A�ao " . $rs6["num_acao"] . ": " . str_replace("\n","", trim($rs6["des_acao"]));
								echo $des_acao . " \t" . trim(pg_fetch_result($resultado9,0,"data"));
							}
							echo "\n";
						}
					}
	} // fim metas

}

mssql_close($conexao);
?>